<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <style>
        .item img {
            width: 150px;
            height: 150px;
            padding: 5px;
        }

        .item {
            border: 1px solid #eee;
            float: left;
            margin-right: 80px;
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <div id="app"></div>
    <script>
        loadData()
        // jump();

        $(window).scroll(function(){
            if(isReachBottom()){
                var offset = $(".item").length;
                loadData(offset)
            }
        })

        function http({
            offset = 0,
            success
        }){
            var url =`http://192.168.4.18:3000/top/playlist?cat=华语&limit=20&offset=${offset}`
            $.ajax({
                url,
                success:res=>{
                    success(res)
                }
            })
        }

        function isReachBottom(){
            var scrollTop = $(window).scrollTop();
            var availHeight = $(window).height();
            var dh = $(document).height();
            return (scrollTop+availHeight == dh)?true:false;
        }

        function jump(){
            setTimeout(() => {
                $(".item").click(function(event){
                    let aid = event.currentTarget.dataset.aid;
                    location.href = `detail.html?id=${aid}`
                })
            }, 200);
        }

        function loadData(offset) {
            http({
                offset,
                success: res => {
                    var playlists = res.playlists;
                    playlists.forEach(item => {
                        var template = `
                         <div class="item" data-aid=${item.id}>
                            <img src=${item.coverImgUrl}>
                         </div>   
                      `
                        $("#app").append(template)
                    })
                }
            })
        }
    </script>
</body>
</html>